/*
 * @LastEditTime: 2023-03-31 23:34:06
 * @Method: 
 * @Description: 
 */
package CodeTop;

public class Solution129 {
    int res = 0;

    public int sumNumbers(TreeNode root) {
        dfs(root, res);
        return res;
    }

    public void dfs (TreeNode node, int sum) {
        if (node == null) {
            return;
        }
        int val = sum * 10 + node.val;
        if (node.left == null && node.right == null) {
            res += val;
            return;
        }
        dfs(node.left, val);
        dfs(node.right, val);
    }
}
